package tk.mybatis.web.controller;

import com.sun.deploy.net.HttpResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.web.model.Person;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/persons")
public class PersonsApiController {
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<Person>> getList() {
        List<Person> persons = new ArrayList<Person>() {
            {
                add(new Person() {{
                    setId(1);
                    setName("张三");
                    setAge(5);
                    setBirthdayString("2015-4-5");
                }});
                Person person = new Person();
                person.setId(2);
                person.setName("李四");
                person.setAge(10);
                person.setBirthdayString("2010-4-8");
                add(person);
            }
        };
        return new ResponseEntity<>(persons, HttpStatus.ACCEPTED);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String post(Person person) {
        return "post";
    }
    @RequestMapping(method = RequestMethod.PUT, consumes="application/x-www-form-urlencoded; charset=UTF-8")
    public String put(String name, Date birthday) {
        return "put";
    }
    @RequestMapping(method = RequestMethod.PUT, consumes = "application/json; charset=utf-8")
    public String putAll(@RequestBody Person person) {
        return "put";
    }
    @RequestMapping(method = RequestMethod.DELETE)
    public String delete(Integer id) {
        return "delete";
    }
}
